<template>
	<div ref="scheduleShiftChart" v-resize="resizeChart" :style="{width: '100%', height: `${height || 300}px`}"></div>
</template>

<script>
    import homeMixin from '../../../../mixins/dom-resize';
    import { borderColor, color, grid, legend } from './chart-conf';
    import * as echarts from 'echarts';

    export default {
        mixins: [homeMixin],
        props: {
            tableData: {
                type: Array,
                default() {
                    return [];
                }
            },
            height: {
                type: [String, Number],
                default: 0
            }
        },
        data() {
            return {
                example: null,
                borderColor: '#c5c8ce',
                option: {
                    title: {
                        text: '',
                        textStyle: {
                            fontWeight: 'none'
                        }
                    },
                    color: ['#2b85e4'],
                    tooltip: {
                        trigger: 'axis',
                        formatter: (params) => {
                            let str = '';
                            params.forEach((item, index) => {
                                str += `<div>
                                            <div style="display: flex;align-items: center;">
                                                <div style="width: 10px;height: 10px;background: ${params[index].color};border-radius: 100%;margin-right: 6px;"></div>
                                                <div>${params[0].axisValue}</div>
                                            </div>
                                            <div style="margin-left: 14px;">${params[index].seriesName}: ${params[index].data}</div>
                                            <div style="margin-left: 14px;">当班产量(M)：${this.tableData[params[0].dataIndex].outputM}</div>
                                            <div style="margin-left: 14px;">${this.tableData[params[0].dataIndex].rotateSpeed || ''}</div>
                                            <div style="margin-left: 14px;">当班开始产量表数(米)：${this.tableData[params[0].dataIndex].startOutputM}</div>
                                            <div style="margin-left: 14px;">当班实时产量表数(米)：${this.tableData[params[0].dataIndex].endOutputM}</div>
                                            <div style="margin-left: 14px;">当班开始能耗(KW.H)表数：${this.tableData[params[0].dataIndex].startEnergy}</div>
                                            <div style="margin-left: 14px;">当班实时能耗(KW.H)表数：${this.tableData[params[0].dataIndex].endEnergy}</div>
                                            <div style="margin-left: 14px;">时间：${this.tableData[params[0].dataIndex].belongDateTime}</div>
                                        </div>`;
                            });
                            return str;
                        }
                    },
                    legend: legend,
                    grid: grid,
                    xAxis: {
                        type: 'category',
                        data: [],
                        axisLabel: {
                            show: true,
                            color: color

                        },
                        nameTextStyle: {
                            color: 'red'
                        },
                        axisLine: {
                            lineStyle: {
                                color: borderColor
                            }
                        }
                    },
                    yAxis: {
                        type: 'value',
                        splitLine: {
                            lineStyle: {
                                color: 'none'
                            }
                        },
                        axisLabel: {
                            show: true,
                            color: color

                        },
                        axisLine: {
                            lineStyle: {
                                color: borderColor
                            }
                        }
                    },
                    series: []
                }
            };
        },
        methods: {
            resize() {
                window.addEventListener('resize', () => {
                    this.resizeChart();
                });
            },
            initChart() {
                this.$nextTick(() => {
                    this.example = echarts.init(this.$refs.scheduleShiftChart);
                    this.updateChart();
                });
            },
            updateChart() {
                this.$nextTick(() => {
                    this.option.series = [
                        {
                            name: '当班产量(kg)',
                            type: 'bar',
                            data: this.tableData && this.tableData.length ? this.tableData.map(x => x.outputKg || 0) : [],
                            smooth: true,
                            symbol: 'none',
                            barGap: 0,
                            lineStyle: {
                                width: 5
                            }
                        }
                    ];
                    this.option.xAxis.data = this.tableData && this.tableData.length ? this.tableData.map(x => x.deviceRecordNumber) : [];
                    this.example.setOption(this.option);
                });
            }
        },
        mounted() {
            this.initChart();
            this.resize();
        },
        watch: {
            tableData: {
                handler(newVal) {
                    this.updateChart();
                },
                deep: true
            }
        }
    };
</script>
